package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.Elefee;
import com.qf.entity.House;
import com.qf.entity.Owner;
import com.qf.entity.Property;
import com.qf.enums.AppHttpCodeEnum;
import com.qf.mapper.ElefeeMapper;
import com.qf.mapper.HouseMapper;
import com.qf.mapper.OwnerMapper;
import com.qf.result.ResponseResult;
import com.qf.service.ElefeeService;
import com.qf.service.HouseService;
import com.qf.service.OwnerService;
import com.qf.utils.BeanCopyUtils;
import com.qf.vo.ElefeeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Elefee)表服务实现类
 *
 * @author makejava
 * @since 2023-02-04 22:19:13
 */
@Service("elefeeService")
@Slf4j
public class ElefeeServiceImpl extends ServiceImpl<ElefeeMapper, Elefee> implements ElefeeService {

    @Autowired
    private OwnerService ownerService;

    @Autowired
    private HouseService houseService;

    @Resource
    private ElefeeMapper elefeeMapper;

    @Resource
    private OwnerMapper ownerMapper;

    @Resource
    private HouseMapper houseMapper;

    @Override
    public ResponseResult test() {
        List<Elefee> elefees = list();
        List<ElefeeVo> elefeeVos = BeanCopyUtils.copyBeanList(elefees, ElefeeVo.class);
        elefeeVos.stream()
                .map(elefeeVo -> elefeeVo.setOwnName(ownerService.getById(elefeeVo.getOwnId()).getOwnName()))
                .map(elefeeVo -> elefeeVo.setHouseNum(houseService.getById(elefeeVo.getHouseId()).getHouseNum()))
                .collect(Collectors.toList());
        return ResponseResult.okResult(elefeeVos);
    }


    @Override
    public ResponseResult findAll(String ownerName) {
        List<Elefee> elefees = new ArrayList<>();

        if (ownerName==null || "".equals(ownerName)) {
            elefees = elefeeMapper.selectList(null);
            for (Elefee elefee:elefees){
                Integer ownId = elefee.getOwnId();
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("id", ownId);
                Owner owner = ownerMapper.selectOne(queryWrapper1);
//                if (owner==null){
//                    continue;
//                }
                log.info("============="+owner);
                elefee.setOwnerName(owner.getOwnName());
            }
        }else {
            QueryWrapper<Owner> queryWrapper = new QueryWrapper();
            queryWrapper.like("own_name", ownerName);
            List<Owner> list = ownerMapper.selectList(queryWrapper);
            for (Owner owner : list) {
                Integer oid = owner.getId();
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("own_id", oid);
                Elefee elefee = elefeeMapper.selectOne(queryWrapper1);
                if (elefee==null){
                    continue;
                }
                elefee.setOwnerName(owner.getOwnName());
                elefees.add(elefee);
            }

            QueryWrapper<Elefee> queryWrapper1 = new QueryWrapper();
            queryWrapper1.like("fee_status","未缴费".contains(ownerName)?0:("已缴费".contains(ownerName)?1:""))
                    .or()
                    .like("house_id",ownerName);
            List<Elefee> elefees1 = elefeeMapper.selectList(queryWrapper1);
            elefees.addAll(elefees1);
        }
        return ResponseResult.okResult(elefees);
    }

    @Override
    @Transactional
    public ResponseResult delete(int id) {
        Elefee elefee = elefeeMapper.selectById(id);
        Double eleFee = elefee.getEleFee();

        int i = elefeeMapper.deleteById(id);
        if (i==1) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult add(Elefee elefee) {
        int i = elefeeMapper.insert(elefee);
        if (i==1) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult findAllHouse() {
        ResponseResult house = houseService.getHouseByTrueStatus();
//        List<House> houses = houseMapper.selectList(null);
        return house;
    }

    @Override
    public ResponseResult findOwnerByHouseId(String house) {
        QueryWrapper<House> queryWrapper = new QueryWrapper();
        queryWrapper.eq("house_num", house);
        House houses = houseMapper.selectOne(queryWrapper);
        Integer ownId = houses.getOwnId();
        Owner owner = ownerMapper.selectById(ownId);
        return ResponseResult.okResult(owner);
    }


}
